package experiment2.task3;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author 冯永龙
 * @date 2021/3/23 14:18
 */
class test {
    public static void main(String[] args) {
        PersonIntf person = new Person();
        MyInvocationHandler handler = new MyInvocationHandler();
        PersonIntf proxy = (PersonIntf) handler.bind(person);

        proxy.setAge(23);
        proxy.setName("张三");
        proxy.setIdNo("89236894234324");
        proxy.setMerried(true);
        proxy.setSex("male");
        writeFile(handler);

    }

    public static void writeFile(MyInvocationHandler handler){
        BufferedWriter bufferedWriter = null;
        String file = "src/experiment2/task3/Person.txt";

        try {
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file,true)));

            ArrayList<String> log = handler.getLog();
            Iterator<String> iterator = log.iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                bufferedWriter.write(next+"\r\n");
                System.out.println(next);
            }
            bufferedWriter.flush();
        } catch (Exception e) {
            System.out.println("写入文件失败");
            e.printStackTrace();
        }finally {
            try {
                bufferedWriter.close();
                System.out.println("写入文件成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
